<?php
/**
 * Created by PhpStorm.
 * User: sen
 * Date: 16/7/27
 * Time: 下午3:45
 */
namespace app\models;

use yii\base\Model;
use yii\web\UploadedFile;

class UploadForm extends Model
{
    /**
     * @var UploadedFile
     */
    public $imageFile;

    public function rules()
    {
//        return [
//            [['imageFile'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg'],
//        ];
        return array('image', 'file', 'allowEmpty'=>true,
            'types'=>'jpg, jpeg, gif, png',
            'maxSize'=>1024 * 1024 * 1, // 1MB
            'tooLarge'=>'上传文件超过 1MB，无法上传。',
        );
    }

    public function upload()
    {
        if ($this->validate()) {
            return 'uploads/' . $this->imageFile->baseName . '.' . $this->imageFile->extension;
            $this->imageFile->saveAs('uploads/' . $this->imageFile->baseName . '.' . $this->imageFile->extension);
//            return true;
        } else {
            return false;
        }
    }
}